<?php
class CustomerAppointment extends AppModel
{
    /**
     * associations
     *
     * @var array
     */

    var $belongsTo = array(
        'CustomerAppointmentParent' => array(
            'className' => 'CustomerAppointmentParent',
            'foreignKey' => 'customer_appointment_parent_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        ),
        'Customer' => array(
            'className' => 'Customer',
            'foreignKey' => 'customer_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        )
    );

    function getReminderById($Id = null, $recursive = -1)
    {
        $conditions = array(
            'CustomerAppointment.id' => $Id,
            'CustomerAppointment.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('first', array('conditions' => $conditions, 'recursive' => $recursive));
    }

    function getRemindersById($Ids = null, $recursive = -1)
    {
        $conditions = array(
            'CustomerAppointment.id' => $Ids,
            'CustomerAppointment.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('all', array('conditions' => $conditions, 'recursive' => $recursive));
    }

    function getCustomerReminders($Id = null, $recursive = -1)
    {
        $conditions = array(
            'CustomerAppointment.customer_id' => $Id,
            'CustomerAppointment.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return $this->find('first', array('conditions' => $conditions, 'recursive' => $recursive));
    }


    function today_reminders_count($type = 'service_customer',$filter_reminded = true){
        //$fields = array('CustomerAppointment.id','CustomerAppointment.customer_id','CustomerAppointment.next_service_date');
        $today = date('Y-m-d');
        $condition_array = array(
            'Customer.type' => $type,
            'Customer.deleted' => 'n',
            'CustomerAppointment.reminder_date' => $today,
            'CustomerAppointment.serviced' => 'Not Serviced',
            'CustomerAppointment.deleted' => 'n'
        );
        if($filter_reminded){
            $condition_array['CustomerAppointment.reminded'] = 'Not Reminded';
        }
        $data_table = $this->find('all', array(
            //'fields'=>$fields,
            'conditions' => $condition_array ,
            'contain'=>array(
                'Customer'=>array('fields'=>array('Customer.id','Customer.name','Customer.telephone','Customer.email'))
            ),
            'recursive' => 1
        ));

        $count = 0;
        $data = array();

        foreach($data_table as $d){
            $count++;
            $data[$d['CustomerAppointment']['customer_id']] = array(
                'id'=>$d['CustomerAppointment']['id'],
                'name'=>$d['Customer']['name'],
                'telephone'=>$d['Customer']['telephone'],
                'email'=>$d['Customer']['email'],
                'next_service_date'=> date("F d, Y", strtotime($d['CustomerAppointment']['next_service_date'])),
                'vehicle_number'=> $d['CustomerAppointment']['vehicle_number'],
                'vehicle_type'=> $d['CustomerAppointment']['vehicle_type'],
                'reminded'=> $d['CustomerAppointment']['reminded'],
                'reminder_count'=> $d['CustomerAppointment']['reminder_count']
            );
        }

        return array(
            'count'=>$count,
            'data'=>$data
        );
    }


    function today_appointments_count($type = 'service_customer',$filter_serviced = true){
        //$fields = array('CustomerAppointment.id','CustomerAppointment.customer_id');
        $today = date('Y-m-d');
        $condition_array = array(
            'Customer.type' => $type,
            'Customer.deleted' => 'n',
            'CustomerAppointment.next_service_date' => $today,
            'CustomerAppointment.deleted' => 'n'
        );
        if($filter_serviced){
            $condition_array['CustomerAppointment.serviced'] = 'Not Serviced';
        }
        $data_table = $this->find('all', array(
            //'fields'=>$fields,
            'conditions' => $condition_array ,
            'contain'=>array(
                'Customer'=>array('fields'=>array('Customer.id','Customer.name','Customer.telephone','Customer.email'))
            ),
            'recursive' => 1
        ));

        $count = 0;
        $data = array();

        foreach($data_table as $d){
            $count++;
            $data[$d['CustomerAppointment']['customer_id']] = array(
                'id'=>$d['CustomerAppointment']['id'],
                'name'=>$d['Customer']['name'],
                'telephone'=>$d['Customer']['telephone'],
                'email'=>$d['Customer']['email'],
                'next_service_date'=> date("F d, Y", strtotime($d['CustomerAppointment']['next_service_date'])),
                'vehicle_number'=> $d['CustomerAppointment']['vehicle_number'],
                'vehicle_type'=> $d['CustomerAppointment']['vehicle_type'],
                'serviced'=> $d['CustomerAppointment']['serviced']
            );
        }

        return array(
            'count'=>$count,
            'data'=>$data
        );
    }


    function getAllAppointments($customer_type = 'service_customer',$start=null,$end=null,$customer=null,$vehicle_type=null,$vehicle_number=null)
    {
        //$fields = array('CustomerAppointment.id','CustomerAppointment.vehicle_number','CustomerAppointment.vehicle_type','CustomerAppointment.customer_id','CustomerAppointment.reminder_time','CustomerAppointment.reminder_date','CustomerAppointment.next_service_date','CustomerAppointment.end_date','CustomerAppointment.reminded','CustomerAppointment.serviced');
        $today = date('Y-m-d');
        $condition_array = array(
            'Customer.type' => $customer_type,
            'Customer.deleted' => 'n',
            'CustomerAppointment.deleted' => 'n'
        );


        if($start != null && $end !=null){
            $condition_array['DATE(CustomerAppointment.created) >=']=$start;//from now
            $condition_array['DATE(CustomerAppointment.created) <=']=$end; //to a given end date
        }
        if($customer != null){
            $condition_array['CustomerAppointment.customer_id'] = $customer;
        }
        if($vehicle_type != null){
            $condition_array['CustomerAppointment.vehicle_type'] = $vehicle_type;
        }
        if($vehicle_number != null){
            $condition_array['CustomerAppointment.vehicle_number LIKE '] = $vehicle_number."%";
        }

        $data_table = $this->find('all', array(
            //'fields'=>$fields,
            'conditions' => $condition_array ,
            'contain'=>array(
                'CustomerAppointmentParent'=>array('fields'=>array('CustomerAppointmentParent.id','CustomerAppointmentParent.repeat','CustomerAppointmentParent.repeat_freq')),
                'Customer'=>array('fields'=>array('Customer.id','Customer.name','Customer.telephone','Customer.email','Customer.dob'))
            ),
            'recursive' => 1
        ));

        $count = 0;
        $data = array();

        foreach($data_table as $d){
            $count++;
            $cmp_re_status = $this->compareDate(date('Y-m-d'),$d['CustomerAppointment']['reminder_date']);
            $cmp_ap_status = $this->compareDate(date('Y-m-d'),$d['CustomerAppointment']['next_service_date']);
            $data[$d['CustomerAppointment']['id']] = array(
                'id'=>$d['CustomerAppointment']['id'],
                'customer_id'=>$d['CustomerAppointment']['customer_id'],
                'name'=>$d['Customer']['name'],
                'dob'=>$d['Customer']['dob'],
                'telephone'=>$d['Customer']['telephone'],
                'email'=>$d['Customer']['email'],
                'vehicle_number'=>$d['CustomerAppointment']['vehicle_number'],
                'vehicle_type'=>$d['CustomerAppointment']['vehicle_type'],
                'reminder_time'=>$d['CustomerAppointment']['reminder_time'],
                'reminder_date'=>$d['CustomerAppointment']['reminder_date'],
                'next_service_date'=>$d['CustomerAppointment']['next_service_date'],
                'end_date'=>$d['CustomerAppointment']['end_date'],
                'serviced'=>$d['CustomerAppointment']['serviced'],
                'service_status'=>$cmp_ap_status,
                'reminded'=>$d['CustomerAppointment']['reminded'],
                'reminder_status'=> $cmp_re_status,
                'parent_id'=>$d['CustomerAppointmentParent']['id'],
                'repeat'=>$d['CustomerAppointmentParent']['repeat'],
                'repeat_freq'=>$d['CustomerAppointmentParent']['repeat_freq']
            );
        }

        return array(
            'count'=>$count,
            'data'=>$data
        );
    }


    function overdue_appointments($type='reminders',$customer_type = 'service_customer'){
       // $fields = array('CustomerAppointment.id','CustomerAppointment.customer_id','CustomerAppointment.reminder_date','CustomerAppointment.next_service_date');
        $today = date('Y-m-d');
        $condition_array = array(
            'Customer.type' => $customer_type,
            'Customer.deleted' => 'n',
            'CustomerAppointment.deleted' => 'n'
        );
        if($type=='reminders'){
            $condition_array['CustomerAppointment.reminder_date < ']=  $today;
            $condition_array['CustomerAppointment.reminded']= 'Not Reminded';
            $condition_array['CustomerAppointment.serviced']= 'Not Serviced';
        }
        elseif($type=='appointments'){
            $condition_array['CustomerAppointment.next_service_date <']=  $today;
            $condition_array['CustomerAppointment.serviced']= 'Not Serviced';
        }
        $data_table = $this->find('all', array(
            //'fields'=>$fields,
            'conditions' => $condition_array ,
            'contain'=>array(
                'Customer'=>array('fields'=>array('Customer.id','Customer.name','Customer.telephone','Customer.email'))
            ),
            'recursive' => 1
        ));

        $count = 0;
        $data = array();

        foreach($data_table as $d){
            $count++;
            $data[$d['CustomerAppointment']['customer_id']] = array(
                'id'=>$d['CustomerAppointment']['id'],
                'name'=>$d['Customer']['name'],
                'telephone'=>$d['Customer']['telephone'],
                'email'=>$d['Customer']['email'],
                'reminder_date'=>$d['CustomerAppointment']['reminder_date'],
                'next_service_date'=>$d['CustomerAppointment']['next_service_date'],
                'vehicle_number'=> $d['CustomerAppointment']['vehicle_number'],
                'vehicle_type'=> $d['CustomerAppointment']['vehicle_type']
            );
        }

        return array(
            'count'=>$count,
            'data'=>$data
        );
    }



    function reportAppointments($date,$customer=null)
    {
        $condition_array = array(
            'CustomerAppointment.next_service_date  LIKE' => $date.'%',
            'CustomerAppointment.deleted' => 'n'
        );
        if($customer != null){
            $condition_array['CustomerAppointment.customer_id'] = $customer;
        }

        $data_table = $this->find('all', array(
            //'fields'=>$fields,
            'conditions' => $condition_array ,
            'contain'=>array(
                'Customer'=>array('fields'=>array('Customer.id','Customer.name','Customer.telephone','Customer.email','Customer.dob'))
            ),
            'recursive' => 1
        ));

        $count = 0;
        $data = array();

        foreach($data_table as $d){
            $count++;
            $turned_up_or_not = $d['CustomerAppointment']['serviced'];
            $data[$turned_up_or_not][] = array(
                'id'=>$d['CustomerAppointment']['id'],
                'customer_id'=>$d['CustomerAppointment']['customer_id'],
                'name'=>$d['Customer']['name'],
                'dob'=>$d['Customer']['dob'],
                'telephone'=>$d['Customer']['telephone'],
                'email'=>$d['Customer']['email'],
                'vehicle_number'=>$d['CustomerAppointment']['vehicle_number'],
                'vehicle_type'=>$d['CustomerAppointment']['vehicle_type'],
                'reminder_time'=>$d['CustomerAppointment']['reminder_time'],
                'reminder_date'=>$d['CustomerAppointment']['reminder_date'],
                'next_service_date'=>$d['CustomerAppointment']['next_service_date'],
                'end_date'=>$d['CustomerAppointment']['end_date'],
                'serviced'=>$turned_up_or_not,
                'turned_up_date'=>date('d/m/Y',strtotime($d['CustomerAppointment']['turned_up_date'])),
                'expected'=>date('d/m/Y',strtotime($d['CustomerAppointment']['next_service_date'])),
                'reminded'=>$d['CustomerAppointment']['reminded']
            );
        }

        return array(
            'count'=>$count,
            'data'=>$data
        );
    }

}